/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PassWordd.java
 *
 * Created on 19 déc. 2011, 23:07:10
 */
package org.beynet.docs.ui;

import java.awt.event.ActionEvent;
import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.TextOutputCallback;
import javax.security.auth.callback.UnsupportedCallbackException;

/**
 *
 * @author beynet
 */
public class PassWord extends javax.swing.JDialog implements CallbackHandler{

    /** Creates new form PassWordd */
    public PassWord(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        value = new javax.swing.JTextField();
        valid = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("jLabel1");

        value.setColumns(50);
        value.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                valueKeyPressed(evt);
            }
        });

        valid.setText("validate");
        valid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                validActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(45, 45, 45)
                        .add(jLabel1)
                        .add(64, 64, 64)
                        .add(value, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 174, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(146, 146, 146)
                        .add(valid)))
                .addContainerGap(72, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(103, 103, 103)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(value, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(68, 68, 68)
                .add(valid)
                .addContainerGap(72, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void validActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_validActionPerformed
// TODO add your handling code here:
    this.setVisible(false);
}//GEN-LAST:event_validActionPerformed

    private void valueKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_valueKeyPressed
        if (evt.getKeyChar()=='\n') {
            this.setVisible(false);
        }
    }//GEN-LAST:event_valueKeyPressed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton valid;
    private javax.swing.JTextField value;
    // End of variables declaration//GEN-END:variables

    @Override
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        for (Callback callback : callbacks) {
            if (callback instanceof TextOutputCallback) {

                // display the message according to the specified type
                TextOutputCallback toc = (TextOutputCallback) callback;
                switch (toc.getMessageType()) {
                    case TextOutputCallback.INFORMATION:
                        jLabel1.setText(toc.getMessage());
                        break;
                    case TextOutputCallback.ERROR:
                        jLabel1.setText("ERROR: " + toc.getMessage());
                        break;
                    case TextOutputCallback.WARNING:
                        jLabel1.setText("WARNING: " + toc.getMessage());
                        break;
                    default:
                        throw new IOException("Unsupported message type: "
                                + toc.getMessageType());
                }
                
            } else if (callback instanceof NameCallback) {

                // prompt the user for a username
                NameCallback nc = (NameCallback)callback;
                jLabel1.setText(nc.getPrompt());
                setVisible(true);
                nc.setName(value.getText());
                value.setText("");
                
            } else if (callback instanceof PasswordCallback) {

                // prompt the user for sensitive information
                PasswordCallback pc = (PasswordCallback) callback;
                jLabel1.setText(pc.getPrompt());
                setVisible(true);
                pc.setPassword(value.getText().toCharArray());
                value.setText("");
                
            } else {
                throw new UnsupportedCallbackException(callback, "Unable to handle this callback");
            }
        }
    }
}
